/**
 * 服务器列表页面;
 */
import uiPanel from "./../../lib/UiPanel";
const { ccclass, property } = cc._decorator;
@ccclass
export default class ServerListPanel extends uiPanel {
  @property(cc.ScrollView)
  sv_servers: cc.ScrollView = null;

  @property(cc.Node)
  clone: cc.Node = null;

  onLoad() {
    var content = this.sv_servers.content;
    var servers = cc.vv.serverList.serverList[0]; //require("ServerList")[0];
    for (var i = 0; i < servers.length; i++) {
      var clone = cc.instantiate(this.clone);
      if (clone) {
        clone.data = servers[i];
        var node_name = clone.getChildByName("lbl_name");
        lbl_name = node_name.getComponent(cc.Label);
        lbl_name.string = servers[i].name || "";

        var node_ip = clone.getChildByName("lbl_ip");
        var lbl_name = node_ip.getComponent(cc.Label);
        lbl_name.string =
          (servers[i].platform_ip || "") +
          ":" +
          (servers[i].platform_port || "");

        clone.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        clone.setPosition(cc.v2(0, 0));
        content.addChild(clone);
      }
    }
  }
  onTouchEnd(event) {
    var target = event.target;
    var data = target.data;

    console.log("select server infos: " + JSON.stringify(data));
    cc.vv.GLNetDataModel.setPlatformCfg(data);

    //cc.vv.GLPageMgr.closePage("Page_ServerList");
    //   cc.vv.Login.Btn_Wechat_OnClick();//refresh();

    cc.vv.UIMgr.close(
      cc.vv.GLConfig.serverPanel_path,
      cc.vv.UIMgr.closeType.destory
    );

    cc.vv.eventCustom.emit("chooseServerEnd");
  }
}
